/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azhar.database;

import azhar.persistance.control.*;
import azhar.persistance.*;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author Al-Insan
 */
public class DatabaseController {

    public GuruJpaController guru;
    public JenjangJpaController jenjang;
    public KaryawanJpaController karyawan;
    public KelasJpaController kelas;
    public SiswaJpaController siswa;
    public InboxJpaController inbox;
    public OutboxJpaController outbox;
    public DraftJpaController draft;
    public SentItemJpaController sentItem;
    private DatabaseConnectivity db;

    public DatabaseController() {
        try {
            if (!checkDatabase()) {
                db.manipulateQuery("create database sms_center");
                db.closeConnection();
                System.out.println("database created");
                doTables();
                insertDataJenjang();
                insertDataKelas();
            } else {
                doTables();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void doTables() {
        guru = new GuruJpaController();
        karyawan = new KaryawanJpaController();
        siswa = new SiswaJpaController();
        kelas = new KelasJpaController();
        jenjang = new JenjangJpaController();
        inbox = new InboxJpaController();
        outbox = new OutboxJpaController();
        draft = new DraftJpaController();
        sentItem = new SentItemJpaController();
    }

    private boolean checkDatabase() throws SQLException {
        db = new DatabaseConnectivity("", "root", "root");
        db.executeQuery("show databases");
        ArrayList data = db.getData("Databases");
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).equals("sms_center")) {
                db.closeConnection();
                return true;
            }
        }
        return false;
    }

    public ArrayList[] getDatasInbox() {
        ArrayList[] data = new ArrayList[inbox.getInboxCount()];
        for (int i = 0; i < inbox.getInboxCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(inbox.findInboxEntities().get(i).getId()));
            data[i].add(String.valueOf(inbox.findInboxEntities().get(i).getPengirim()));
            data[i].add(String.valueOf(inbox.findInboxEntities().get(i).getIsiPesan()));
            data[i].add(String.valueOf(inbox.findInboxEntities().get(i).getWaktu()));
            data[i].add(String.valueOf(inbox.findInboxEntities().get(i).getSmsc()));
            data[i].add(String.valueOf(inbox.findInboxEntities().get(i).getStatus()));
        }
        return data;
    }

    public ArrayList[] getDatasOutbox() {
        ArrayList[] data = new ArrayList[outbox.getOutboxCount()];
        for (int i = 0; i < outbox.getOutboxCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(outbox.findOutboxEntities().get(i).getIdPesan()));
            data[i].add(String.valueOf(outbox.findOutboxEntities().get(i).getPenerima()));
            data[i].add(String.valueOf(outbox.findOutboxEntities().get(i).getPesan()));
            data[i].add(String.valueOf(outbox.findOutboxEntities().get(i).getStatus()));
        }
        return data;
    }

    public ArrayList[] getDatasDraft() {
        ArrayList[] data = new ArrayList[draft.getDraftCount()];
        for (int i = 0; i < draft.getDraftCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(draft.findDraftEntities().get(i).getIdPesan()));
            data[i].add(String.valueOf(draft.findDraftEntities().get(i).getPenerima()));
            data[i].add(String.valueOf(draft.findDraftEntities().get(i).getPesan()));
        }
        return data;
    }

    public ArrayList[] getDatasSent() {
        ArrayList[] data = new ArrayList[sentItem.getSentItemCount()];
        for (int i = 0; i < sentItem.getSentItemCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(sentItem.findSentItemEntities().get(i).getIdPesan()));
            data[i].add(String.valueOf(sentItem.findSentItemEntities().get(i).getPenerima()));
            data[i].add(String.valueOf(sentItem.findSentItemEntities().get(i).getPesan()));
            data[i].add(String.valueOf(sentItem.findSentItemEntities().get(i).getStatus()));
            data[i].add(String.valueOf(sentItem.findSentItemEntities().get(i).getWaktu()));
        }
        return data;
    }

    public ArrayList[] getDatasGuru() {
        ArrayList[] data = new ArrayList[guru.getGuruCount()];
        for (int i = 0; i < guru.getGuruCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(guru.findGuruEntities().get(i).getIdGuru()));
            data[i].add(String.valueOf(guru.findGuruEntities().get(i).getNip()));
            data[i].add(String.valueOf(guru.findGuruEntities().get(i).getNamaGuru()));
            data[i].add(String.valueOf(guru.findGuruEntities().get(i).getAlamat()));
            data[i].add(String.valueOf(guru.findGuruEntities().get(i).getNoHp()));
            data[i].add(String.valueOf(jenjang.findJenjang(guru.findGuruEntities().get(i).getIdJenjang()).getNamaJenjang()));
        }
        return data;
    }

    public ArrayList[] getDatasKaryawan() {
        ArrayList[] data = new ArrayList[karyawan.getKaryawanCount()];
        for (int i = 0; i < karyawan.getKaryawanCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(karyawan.findKaryawanEntities().get(i).getIdKaryawan()));
            data[i].add(String.valueOf(karyawan.findKaryawanEntities().get(i).getNamaKaryawan()));
            data[i].add(String.valueOf(karyawan.findKaryawanEntities().get(i).getNoHp()));
            data[i].add(String.valueOf(karyawan.findKaryawanEntities().get(i).getAlamat()));
        }
        return data;
    }

    public ArrayList[] getDatasSiswa() {
        ArrayList[] data = new ArrayList[siswa.getSiswaCount()];
        for (int i = 0; i < siswa.getSiswaCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(siswa.findSiswaEntities().get(i).getIdSiswa()));
            data[i].add(String.valueOf(siswa.findSiswaEntities().get(i).getNis()));
            data[i].add(String.valueOf(siswa.findSiswaEntities().get(i).getNamaSiswa()));
            data[i].add(String.valueOf(kelas.findKelas(siswa.findSiswaEntities().get(i).getCodeKelas()).getNamaKelas()));
            data[i].add(String.valueOf(siswa.findSiswaEntities().get(i).getAlamat()));
            data[i].add(String.valueOf(siswa.findSiswaEntities().get(i).getNamaOrtu()));
            data[i].add(String.valueOf(siswa.findSiswaEntities().get(i).getNoHp()));
        }
        return data;
    }

    public ArrayList[] getDatasKelas() {
        ArrayList[] data = new ArrayList[kelas.getKelasCount()];
        for (int i = 0; i < kelas.getKelasCount(); i++) {
            data[i] = new ArrayList();
            data[i].add(String.valueOf(kelas.findKelasEntities().get(i).getCodeKelas()));
            data[i].add(String.valueOf(kelas.findKelasEntities().get(i).getNamaKelas()));
            data[i].add(String.valueOf(jenjang.findJenjang(kelas.findKelasEntities().get(i).getCodeJejang()).getNamaJenjang()));
        }
        return data;
    }

    public String[] getJenjangNames() {
        String[] data = new String[jenjang.getJenjangCount()];
        for (int i = 0; i < jenjang.getJenjangCount(); i++) {
            data[i] = String.valueOf(jenjang.findJenjangEntities().get(i).getNamaJenjang());
        }
        return data;
    }

    public String[] getKelasNames() {
        String[] data = new String[kelas.getKelasCount()];
        for (int i = 0; i < kelas.getKelasCount(); i++) {
            data[i] = String.valueOf(kelas.findKelasEntities().get(i).getNamaKelas());
        }
        return data;
    }

    public int getLastIdInbox() {
        int id = 0;
        for (int i = 0; i < inbox.getInboxCount(); i++) {
            id = inbox.findInboxEntities().get(i).getId();
        }
        return id;
    }

    public int getLastIdOutbox() {
        int id = 0;
        for (int i = 0; i < outbox.getOutboxCount(); i++) {
            id = outbox.findOutboxEntities().get(i).getIdPesan();
        }
        return id;
    }

    public int getLastIdDraft() {
        int id = 0;
        for (int i = 0; i < draft.getDraftCount(); i++) {
            id = draft.findDraftEntities().get(i).getIdPesan();
        }
        return id;
    }

    public int getLastIdSent() {
        int id = 0;
        for (int i = 0; i < sentItem.getSentItemCount(); i++) {
            id = sentItem.findSentItemEntities().get(i).getIdPesan();
        }
        return id;
    }

    public int getLastIdGuru() {
        int id = 0;
        for (int i = 0; i < guru.getGuruCount(); i++) {
            id = guru.findGuruEntities().get(i).getIdGuru();
        }
        return id;
    }

    public int getLastIdSiswa() {
        int id = 0;
        for (int i = 0; i < siswa.getSiswaCount(); i++) {
            id = siswa.findSiswaEntities().get(i).getIdSiswa();
        }
        return id;
    }

    public int getLastIdKaryawan() {
        int id = 0;
        for (int i = 0; i < karyawan.getKaryawanCount(); i++) {
            id = karyawan.findKaryawanEntities().get(i).getIdKaryawan();
        }
        return id;
    }

    public static void main(String[] args) {
        DatabaseController db = new DatabaseController();
        for (int i = 0; i < db.getDatasSiswa().length; i++) {
            System.out.println(db.getDatasSiswa()[i].get(0));
            System.out.println(db.getDatasSiswa()[i].get(1));
            System.out.println(db.getDatasSiswa()[i].get(2));
            System.out.println(db.getDatasSiswa()[i].get(3));
        }
        System.out.println("" + db.getLastIdGuru());
        db.inbox.create(new Inbox(0, "sd", "jkh", "jhg", "hjgh", 0));
    }

    private void insertDataKelas() {
        String[] kelasTK = {"Taman Azhar (TA)", "TK A1", "TK A2", "TK B1", "TK B2"};
        String[] kelasSD = {"SD 1A", "SD 1B", "SD 1C", "SD 2A", "SD 2B", "SD 2C", "SD 3A", "SD 3B", "SD 3C", "SD 4A", "SD 4B", "SD 4C", "SD 5A", "SD 5B", "SD 5C", "SD 6A", "SD 6B", "SD 6C"};
        String[] kelasSMP = {"SMP 7A", "SMP 7B", "SMP 8A", "SMP 8B", "SMP 9A", "SMP 9B"};
        for (int i = 0; i < kelasTK.length; i++) {
            kelas.create(new Kelas((i + 1), kelasTK[i], 1));
        }
        for (int i = 0; i < kelasSD.length; i++) {
            kelas.create(new Kelas((i + 6), kelasSD[i], 2));
        }
        for (int i = 0; i < kelasSMP.length; i++) {
            kelas.create(new Kelas((i + 24), kelasSMP[i], 3));
        }
    }

    private void insertDataJenjang() {
        String[] namaJenjang = {"TK", "SD", "SMP"};
        for (int i = 0; i < namaJenjang.length; i++) {
            jenjang.create(new Jenjang((i + 1), namaJenjang[i]));
        }
    }
}
